<template>
  <div class="app">
    <div class="container">
      <div class="login_box">
        <div class="avatar_box">
          <img
            src="http://destiny001.gitee.io/admin-dist/img/logo.b5a855ee.png"
            alt="黑马"
          />
        </div>
        <el-form
          :model="form"
          required
          :rules="rules"
          ref="form"
          status-icon
          class="demo-ruleForm"
        >
          <el-form-item prop="username">
            <el-input
              prefix-icon="el-icon-s-custom"
              inline-message="true"
              v-model="form.username"
              type="text"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              inline-message="true"
              prefix-icon="el-icon-lock"
              type="password"
            ></el-input>
          </el-form-item>
          <el-form-item class="btn">
            <el-button type="primary" @click="submitForm(form)">提交</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/login.js'

export default {
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },

  methods: {
    async submitForm (form) {
      if (this.form.username === '' || this.form.password === '') {
        return this.$refs.form.validate((valid) => {
          // console.log(valid)
          if (!valid) {
            return false
          }
        })
      }
      try {
        const res = await login(this.form)
        console.log(res.data.meta)
        if (res.data.meta.status === 200) {
          this.$store.commit('initUser', res.data.data)
          this.$router.push('/home')
          this.$message({
            message: '登录成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '账户名或者密码错误',
            type: 'warning'
          })
        }
      } catch (err) {
        console.log(err)
      }
    },
    resetForm () {
      this.form.username = 'admin'
      this.form.password = '123456'
    }
  }
}
</script>

<style scoped lang="less">
.app {
  height: 100%;
  margin: 0;
  padding: 0;
  min-width: 1366px;
  background-color: #2b4b6b;
  .container {
    background-color: #2b4b6b;
    height: 100%;
    .login_box {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 450px;
      height: 300px;
      background-color: #fff;
      border-radius: 3px;
      .avatar_box {
        width: 130px;
        height: 130px;
        border: 1px solid #eee;
        background-color: #fff;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0 0 10px #ddd;
        position: absolute;
        left: 50%;
        transform: translate(-50%, -50%);
        img {
          width: 100%;
          height: 100%;
          border-radius: 50%;
          background-color: #eee;
        }
      }
      .el-form {
        margin-top: 120px;
        margin-left: 0;
      }
      /deep/.el-form-item__content {
        margin: 0 30px;
      }
      .btn {
        margin-left: 240px;
      }
    }
  }
}
</style>
